#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# shared library versions, option 1
#version=2.0.5
#major=2
# option 2, assuming the library is created as src/.libs/libfoo.so.2.0.5 or so
me_version=`ls gsmlib/.libs/libgsmme*.so.* | \
 awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'`
me_major=`ls gsmlib/.libs/libgsmme*.so.* | \
 awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'`
ex_version=`ls ext/.libs/libgsmext*.so.* | \
 awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) print substr($$0,RSTART)}'`
ex_major=`ls ext/.libs/libgsmext*.so.* | \
 awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}'`


export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


# FOR AUTOCONF 2.13 ONLY
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += $(DEB_HOST_GNU_TYPE)
else
  confflags += --host $(DEB_BUILD_GNU_TYPE) --build $(DEB_HOST_GNU_TYPE)
endif

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
CFLAGS += -g
CXXFLAGS += -g
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" INSTALL_PROGRAM=$(INSTALL_PROGRAM) \
	./configure $(confflags) --prefix=/usr --mandir=\$${prefix}/share/man \
	--infodir=\$${prefix}/share/info

	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f build-stamp configure-stamp config.log config.status po/de.gmo

	-test -r /usr/share/misc/config.sub && \
	   cp -f /usr/share/misc/config.sub scripts/config.sub
	-test -r /usr/share/misc/config.guess && \
	   cp -f /usr/share/misc/config.guess scripts/config.guess

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_installdirs -pgsm-utils

	# Add here commands to install the package into debian/gsmlib.
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
	chrpath -d debian/tmp/usr/bin/*
	cp contrib/gsmsmsspool contrib/gsmsmsrequeue debian/tmp/usr/bin


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	#
	# build libgsmlib${major} package by moving files from gsmlib-dev
	#
	dh_movefiles -plibgsmme$(me_major)c2a  \
	usr/lib/libgsmme.so.$(me_major)        \
	usr/lib/libgsmme.so.$(me_version)    \
	usr/lib/libgsmext.so.$(ex_major)        \
	usr/lib/libgsmext.so.$(ex_version)    \
	usr/share/locale

	dh_movefiles -plibgsmme-dev     \
	usr/include                   \
	usr/lib

	dh_movefiles -pgsm-utils \
	usr/bin


	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installinit
	dh_installcron
	dh_installman -pgsm-utils debian/*.1 debian/tmp/usr/share/man/man*/*
	dh_installinfo
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs -V
	dh_installdeb
	dh_shlibdeps -ldebian/libgsmme1c2a/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
